package com.leyou.item.controller;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.leyou.item.entity.Category;
import com.leyou.item.servcie.CategoryService;
import com.leyou.pojo.dto.CategoryDto;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    //根据id查询商品种类
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDto>  queryCategoryById(@PathVariable("id") String id){
        Category category = categoryService.getById(id);
        CategoryDto categoryDto = new CategoryDto(category);
        return ResponseEntity.ok(categoryDto);
    }
    //根据id的集合查询商品种类的集合
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDto>>  queryCategoriesByIds(@RequestParam("ids") List<Long> ids){
        List<Category> list = categoryService.listByIds(ids);
        List<CategoryDto> categoryDtoList = list.stream().map(CategoryDto::new).collect(Collectors.toList());
        return ResponseEntity.ok(categoryDtoList);
    }
    //根据品牌id查询商品分类的集合
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDto>>  queryCategoryByBrandId(@RequestParam("id") Long id){
      List<CategoryDto>  categoryDtoList =  categoryService.queryCategoryByBrandId(id);
      return ResponseEntity.ok(categoryDtoList);
    }

    //根据父类id查询子类的集合
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDto>>  queryChildCategoryByParentId(@RequestParam("pid") Long pid){
        List<Category> categoryList = categoryService.query().eq("parent_id", pid).list();
        List<CategoryDto> categoryDtoList = categoryList.stream().map(CategoryDto::new).collect(Collectors.toList());
        return ResponseEntity.ok(categoryDtoList);
    }
}
